/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddUsersToProjectForm.java
 *
 * Created on Apr 17, 2011, 10:02:04 PM
 */

package View;

import Client.Client;
import Module.Team;
import Module.User;
import Module.addUserRoles;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


/**
 *
 * @author CCD gruppe 2
 */
public class AddUsersToProjectFrame extends javax.swing.JFrame {
    private javax.swing.JButton addUserButton;
    private javax.swing.JButton deleteTeamButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton newTeamButton;
    private javax.swing.JButton newUserButton;
    private javax.swing.JButton removeUserButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JTabbedPane teamsTabbedPane;
    private javax.swing.JList teamMembersList;
    private javax.swing.JTextField teamNameTextField;
    private javax.swing.JTextArea userExpertiseTextArea;
    private javax.swing.JList usersList;
    private ArrayList<TeamInfoPanel> teamInfoPanels = new ArrayList<TeamInfoPanel>();
    private ArrayList<User> users = new ArrayList<User>();
    private DefaultListModel usersListModel;
    private int selectedTeam=0;


    /** Creates new form AddUsersToProjectForm */
    public AddUsersToProjectFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        usersList = new javax.swing.JList();
        teamsTabbedPane = new javax.swing.JTabbedPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        teamMembersList = new javax.swing.JList();
        teamNameTextField = new javax.swing.JTextField();
        deleteTeamButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        newUserButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        userExpertiseTextArea = new javax.swing.JTextArea();
        addUserButton = new javax.swing.JButton();
        removeUserButton = new javax.swing.JButton();
        newTeamButton = new javax.swing.JButton();
        usersListModel = new DefaultListModel();
        usersList.setModel(usersListModel);
        teamsTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        jScrollPane1.setViewportView(usersList);


        jScrollPane2.setViewportView(teamMembersList);

        teamNameTextField.setText("Teamnavn");

        deleteTeamButton.setText("Slett Team");
        closeButton.setText("Lukk");

        jLabel1.setText("Brukere:");

        jLabel2.setText("Brukerekspertise");

        newUserButton.setText("Ny Bruker");

        userExpertiseTextArea.setColumns(20);
        userExpertiseTextArea.setLineWrap(true);
        userExpertiseTextArea.setRows(5);
        jScrollPane3.setViewportView(userExpertiseTextArea);

        addUserButton.setText(">>>>>>");

        removeUserButton.setText("<<<<<<");

        newTeamButton.setText("Nytt team");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                    .add(jLabel1)
                    .add(newUserButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel2)
                            .add(removeUserButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                            .add(addUserButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)))
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(layout.createSequentialGroup()
                        .add(21, 21, 21)
                        .add(newTeamButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(closeButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(teamsTabbedPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 342, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(teamsTabbedPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 482, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(newUserButton)
                            .add(newTeamButton)
                            .add(closeButton)))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 138, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(addUserButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(removeUserButton))
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 407, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
        setCenterPos(this);
    }// </editor-fold>

    private static void setCenterPos(JFrame frame) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((screen.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((screen.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }

    /**
     * A method that adds a team tab to a team info panel.
     * @param teamInfoPanel
     * @param teamId
     */
    public void addTeamTab(TeamInfoPanel teamInfoPanel, int teamId){
        int teamNr = teamsTabbedPane.getTabCount();
        teamInfoPanel.setTeamId(teamId);
        teamsTabbedPane.addTab("Team " +(teamNr+1), teamInfoPanel);
        teamInfoPanels.add(teamInfoPanel);
        
    }


    /*
     * Getters and setters
     */

    public JButton getAddUserButton() {
        return addUserButton;
    }

    public JLabel getjLabel1() {
        return jLabel1;
    }

    public JLabel getjLabel2() {
        return jLabel2;
    }

    public JScrollPane getjScrollPane1() {
        return jScrollPane1;
    }

    public JScrollPane getjScrollPane3() {
        return jScrollPane3;
    }

    public JButton getNewTeamButton() {
        return newTeamButton;
    }

    public JButton getNewUserButton() {
        return newUserButton;
    }

    public JButton getRemoveUserButton() {
        return removeUserButton;
    }


    public JTextArea getUserExpertiseTextArea() {
        return userExpertiseTextArea;
    }

    public JList getUsersList() {
        return usersList;
    }

    public DefaultListModel getUsersListModel() {
        return usersListModel;
    }

    public void setUsersListModel(DefaultListModel usersListModel) {
        this.usersListModel = usersListModel;
    }

    public void setTeamsInfoPanels(ArrayList<TeamInfoPanel> teamInfoPanels) {
        this.teamInfoPanels = teamInfoPanels;
    }

    public int getSelectedTeam() {
        return selectedTeam;
    }

    public void setSelectedTeam(int selectedTeam) {
        this.selectedTeam = selectedTeam;
    }

    public JButton getDeleteTeamButton() {
        return deleteTeamButton;
    }

    public JScrollPane getjScrollPane2() {
        return jScrollPane2;
    }

    public JButton getCloseButton() {
        return closeButton;
    }

    public ArrayList<TeamInfoPanel> getTeamInfoPanels() {
        return teamInfoPanels;
    }

    public JList getTeamMembersList() {
        return teamMembersList;
    }

    public JTextField getTeamNameTextField() {
        return teamNameTextField;
    }

    public JTabbedPane getTeamsTabbedPane() {
        return teamsTabbedPane;
    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public void setUsers(ArrayList<User> users) {
        this.users = users;
    }
    public void clearInput(){
        teamInfoPanels.clear();
        teamsTabbedPane.removeAll();
        teamMembersList.removeAll();
        users.clear();
        usersListModel.removeAllElements();
        selectedTeam=0;
        teamNameTextField.setText("");
        userExpertiseTextArea.setText("");
    }

    /**
     * A method that removes some elements that shouldn't show when it's an info panel.
     */
    public void isInfoPanel(){
        this.addUserButton.setVisible(false);
        this.removeUserButton.setVisible(false);
        this.deleteTeamButton.setText("Rediger");
        this.newTeamButton.setVisible(false);
        this.newUserButton.setVisible(false);

    }

    /**
     * A method that shows the elements
     */
    public void editButtonPressed(){
        this.usersList.setVisible(true);
        this.teamMembersList.setVisible(true);
        this.addUserButton.setVisible(true);
        this.removeUserButton.setVisible(true);
        this.deleteTeamButton.setVisible(true);
        this.newTeamButton.setVisible(true);
        this.newUserButton.setVisible(true);
        this.userExpertiseTextArea.setVisible(true);
        this.jScrollPane1.setVisible(true);
        this.jScrollPane3.setVisible(true);
        this.jLabel1.setVisible(true);
        this.jLabel2.setVisible(true);
        for(int i=0; i<teamInfoPanels.size();i++){
            if(i==0){
                teamInfoPanels.get(i).getDeleteTeamButton().setVisible(false);
                teamInfoPanels.get(i).getFasilitatorCheckBox().setFocusable(true);
                teamInfoPanels.get(i).getSecretaryCheckBox().setFocusable(true);
                teamInfoPanels.get(i).getCustomerCheckBox().setFocusable(true);
            }
            else{
                teamInfoPanels.get(i).getTeamNameTextField().setFocusable(true);
                teamInfoPanels.get(i).getDeleteTeamButton().setText("Slett Team");
            }
        }
        
    }

    /**
     * A method that shows the teams.
     * @param team
     * @param u
     * @param client
     */
    public void showTeam(Team team, ArrayList<User> u,Client client){
        addTeamTab(new TeamInfoPanel(), team.getId());
        for(int i=0; i<u.size();i++){
            teamInfoPanels.get(teamInfoPanels.size()-1).getTeamMemberListModel().addElement(u.get(i).getFullname());
            teamInfoPanels.get(teamInfoPanels.size()-1).getUsers().add(u.get(i));
            ArrayList<addUserRoles> userroles = new ArrayList<addUserRoles>();
            ArrayList<String> roles = client.getRoleToUserProject(u.get(i).getId(), client.getSelectedProject().getId());
            for(int j=0; j<roles.size();j++){
                //Check if the user already is added
                boolean added=false;
                if(userroles.size()>0){
                    for(int k=0; k<userroles.size();k++){
                        if(userroles.get(k).getUser().getUsername().equals(u.get(i).getUsername())){
                            added=true;
                            break;
                        }
                    }
                    if(!added){
                        userroles.add(new addUserRoles(u.get(i),false,false,false));
                    }
                }
                else userroles.add(new addUserRoles(u.get(i),false,false,false));
                if(roles.get(j).equals("Fasilitator"))userroles.get(userroles.size()-1).setFasilitator(true);
                if(roles.get(j).equals("Sekretær"))userroles.get(userroles.size()-1).setSecretary(true);
                if(roles.get(j).equals("Kunde"))userroles.get(userroles.size()-1).setCustomer(true);
            }
            for(int j=0; j<userroles.size();j++){
                teamInfoPanels.get(teamInfoPanels.size()-1).getUserRoles().add(userroles.get(j));
            }
            int index=0;

            for(int j=0; j<users.size();j++){
                if(u.get(i).getUsername().equals(users.get(j).getUsername())){
                    index =j;
                    users.remove(index);

                    break;
                }
            }
            if(teamInfoPanels.size()==1){
                teamInfoPanels.get(0).getFasilitatorCheckBox().setVisible(true);
                teamInfoPanels.get(0).getSecretaryCheckBox().setVisible(true);
                teamInfoPanels.get(0).getCustomerCheckBox().setVisible(true);
            }
        }
            teamInfoPanels.get(teamInfoPanels.size()-1).getTeamNameTextField().setText(team.getName());
            teamInfoPanels.get(teamInfoPanels.size()-1).getTeamNameTextField().setFocusable(false);
            teamInfoPanels.get(teamInfoPanels.size()-1).getDeleteTeamButton().setText("Rediger");
    }

    

}
